package com.egene.tw.activity.member;

import android.content.Intent;
import android.net.Uri;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.egene.tw.R;
import com.egene.tw.api.EgeneApi;
import com.egene.tw.dao.UserDao;
import com.egene.tw.model.net.ResponseBean;
import com.egene.tw.model.report.CheckingReportList;
import com.egene.tw.model.report.ClinicalReport;
import com.egene.tw.model.report.ClinicalReportsSuggest;
import com.egene.tw.model.report.CompleteReport;
import com.egene.tw.model.report.DetailReport;
import com.egene.tw.model.report.ReportList;
import com.egene.tw.model.report.v2.CheckItemV2;
import com.egene.tw.model.report.v2.DetailItemV2;
import com.egene.tw.utils.JsonUtil;
import com.egene.tw.utils.MToast;
import com.google.gson.reflect.TypeToken;
import com.loopj.android.http.AsyncHttpResponseHandler;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;
import org.apache.http.Header;

import java.util.List;

@EFragment(R.layout.fragment_check_report)
public class CheckReportFragment extends Fragment {
    @ViewById
    LinearLayout llCompleteReport;
    @ViewById
    LinearLayout llCheckReport;
    @ViewById
    LinearLayout llClinicalReport;


    @Bean
    UserDao userDao;

    LayoutInflater mInflater;

    @AfterViews
    void initView() {
        mInflater = LayoutInflater.from(getActivity());
        String id = userDao.getUser().getUserID();
        EgeneApi.getUserReport(id, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                if(llCheckReport == null){
                    return;
                }
                ResponseBean<ReportList> result = JsonUtil.toBean(responseBody,
                        new TypeToken<ResponseBean<ReportList>>() {
                        }.getType());
                if (result != null && result.getData() != null) {
                    updateCompleteReport(result.getData().getCompletedlist());
                    updateCheckingListReport(result.getData().getCheckinglist2());
                    updateClinicalReport(result.getData().getClinicalreportlist());
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                MToast.show(R.string.no_network);
            }
        });
    }


//    private void updateCheckingListReport(List<CheckingReportList> list) {
//        if (list == null) {
//            return;
//        }
//        for (CheckingReportList item : list) {
//            View view = mInflater.inflate(R.layout.layout_checking_report_list, llCheckReport, false);
//            TextView tvReportNo = (TextView) view.findViewById(R.id.tvReportNo);
//            tvReportNo.setText(item.getReportno());
//            LinearLayout llDetailReportList = (LinearLayout) view.findViewById(R.id.llDetailReportList);
//            List<DetailReport> detailReportList = item.getDetaillist();
//            if (detailReportList != null) {
//                for (DetailReport detailReport : item.getDetaillist()) {
//                    View detailView = mInflater.inflate(R.layout.item_detail_report, llDetailReportList, false);
//                    TextView tvReportName = (TextView) detailView.findViewById(R.id.tvReportName);
//                    TextView tvDate = (TextView) detailView.findViewById(R.id.tvDate);
//                    tvReportName.setText(detailReport.getProgressdesc());
//                    tvDate.setText(detailReport.getShortdate());
//                    llDetailReportList.addView(detailView);
//                }
//            }
//            llCheckReport.addView(view);
//        }
//    }

    private void updateCheckingListReport(List<CheckItemV2> list) {
        if (list == null) {
            return;
        }
        for (CheckItemV2 item : list) {
            View view = mInflater.inflate(R.layout.layout_checking_report_list, llCheckReport, false);
            TextView tvReportNo = (TextView) view.findViewById(R.id.tvReportNo);
            tvReportNo.setText(item.getReportno());
            LinearLayout llDetailReportList = (LinearLayout) view.findViewById(R.id.llDetailReportList);
            List<DetailItemV2> detailReportList = item.getDetaillist2();
            if (detailReportList != null) {
                for (DetailItemV2 detailReport : item.getDetaillist2()) {
                    View detailView = mInflater.inflate(R.layout.item_detail_report, llDetailReportList, false);
                    TextView tvNo = (TextView) detailView.findViewById(R.id.tvNo);
                    TextView tvReportName = (TextView) detailView.findViewById(R.id.tvReportName);
//                    TextView tvDate = (TextView) detailView.findViewById(R.id.tvDate);
                    tvNo.setText(detailReport.getNo());
                    tvReportName.setText(detailReport.getContent());
//                    tvDate.setText(detailReport.getShortdate());
                    llDetailReportList.addView(detailView);
                    if(detailReport.getIscompleted() == 1){
                        tvNo.setBackgroundResource(R.drawable.style_check_r);
                    }else {
                        tvNo.setBackgroundResource(R.drawable.style_check_r_1);
                    }
                }
            }
            if(llCheckReport == null){
                return;
            }
            llCheckReport.addView(view);
        }
    }

    private void updateCompleteReport(List<CompleteReport> list) {
        if (list == null) {
            return;
        }
        for (final CompleteReport item : list) {
            View view = mInflater.inflate(R.layout.item_complete_report, null);
            TextView tvDate = (TextView) view.findViewById(R.id.tvDate);
            TextView tvReportNo = (TextView) view.findViewById(R.id.tvReportNo);
            TextView tvSeeDetail = (TextView) view.findViewById(R.id.tvSeeDetail);
            tvDate.setText(item.getShortdate());
            tvReportNo.setText(item.getReportno());
            tvSeeDetail.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ReportDetailActivity_.intent(getActivity()).reportNo(item.getOrderid()).start();
                }
            });

            if(llCompleteReport == null){
                return;
            }
            llCompleteReport.addView(view);
        }
    }

    //    llClinicalReport
    private void updateClinicalReport(List<ClinicalReport> list) {
        if (list == null || list.size() == 0) {
            return;
        }
        for (final ClinicalReport clinicalReport : list) {
            View view = mInflater.inflate(R.layout.item_clinical_report, llClinicalReport, false);
            TextView tvReportTitle = (TextView) view.findViewById(R.id.tvReportTitle);
            TextView btnOpen = (TextView) view.findViewById(R.id.btnOpen);
            final LinearLayout llClinicalReportList = (LinearLayout) view.findViewById(R.id.llClinicalReportList);

//            tvReportTitle.setText(clinicalReport.getReportname());
            tvReportTitle.setText("臨床報告-" + clinicalReport.getReportdatetime());
            btnOpen.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(clinicalReport.getReportpath()));
                    startActivity(browserIntent);
                }
            });
            tvReportTitle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    llClinicalReportList.setVisibility(llClinicalReportList.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
                }
            });

            if(llClinicalReport == null){
                return;
            }
            llClinicalReport.addView(view);

            List<ClinicalReportsSuggest> suggests = clinicalReport.getSuggestlist();
            if (suggests == null || suggests.size() == 0) {
                continue;
            }
            for (ClinicalReportsSuggest suggest : suggests) {
                View subView = mInflater.inflate(R.layout.item_doctor_suggest, llClinicalReport, false);
                TextView tvDoctor = (TextView) subView.findViewById(R.id.tvDoctor);
                TextView tvContent = (TextView) subView.findViewById(R.id.tvContent);
                TextView tvDate = (TextView) subView.findViewById(R.id.tvDate);

                tvDoctor.setText(suggest.getDoctorname());
                tvContent.setText(suggest.getSuggest());
                tvDate.setText(suggest.getSuggestdate());
                llClinicalReportList.addView(subView);
            }
        }
    }
}
